using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SQLDAL;

public partial class GUI_Webpage_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RestaurantBll resBll = new RestaurantBll();
            resBll.LoadRestaurant(drpRestaurant);
            txtDateUsingTable.Text = DateTime.Today.ToShortDateString();
        }
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        BookingTableDetailsInfo bookingInfo = new BookingTableDetailsInfo();
        RestaurantInfo resInfo = new RestaurantInfo();
        bookingInfo.DateUsingTable = DateTime.Parse(txtDateUsingTable.Text);
        resInfo.ResID = drpRestaurant.SelectedValue;

        LoadData(bookingInfo, resInfo);
    }
    protected void btnAddtoBooking_Click(object sender, EventArgs e)
    {

    }

    #region Load all table which is available of a restauran by a date selected
    private void LoadData(BookingTableDetailsInfo bookingInfo, RestaurantInfo resInfo)
    {
        BookingTableBll bookingBll = new BookingTableBll();
        // declare variable startRecord to save the record is started
        int startRecord = (Int32.Parse(txtCurentPage.Text) - 1) * Int32.Parse(txtPageSize.Text);
        // maxRecord is the max record is displayed in a page
        int maxRecord = Int32.Parse(txtPageSize.Text);

        SqlParameter[] cmdParms = {
            new SqlParameter("@dateUsingTable", SqlDbType.DateTime),
            new SqlParameter("@timeUsing", SqlDbType.VarChar,8),
            new SqlParameter("@resID", SqlDbType.VarChar, 10)
        };
        cmdParms[0].Value = bookingInfo.DateUsingTable;
        cmdParms[1].Value = bookingInfo.TimeUsing;
        cmdParms[2].Value = resInfo.ResID;

        DataSet ds = SQLHelper.GetDataSetUsePaging(CommandType.StoredProcedure,
            "sp_BookingTableDetails_ListTableAvailableByDateOfRestaurant", "BookingTableDetails",
            startRecord, maxRecord, cmdParms);

        //bind data to datalist dlTableInfo
        dlTableInfo.DataSource = ds.Tables["BookingTableDetails"];
        dlTableInfo.DataBind();

        txtTotal.Text = bookingBll.ToTalTableAvailable(bookingInfo, resInfo).ToString();
        BuildPagers();
    }
    #endregion

    #region Method using to navigate the page
    #endregion
    
    #region This method will do when First/Prev/Next/Last link button is clicked
    protected void Page_DataList(object sender, EventArgs e)
    {
         //Check for Button clicked
        if (((LinkButton)sender).ID == "Prev")
        {
            //Check if we are on any page greater than 0 
            if ((int.Parse(txtCurentPage.Text) - 1) >= 0)
            {
                //Decrease the CurrentPage Value
                txtCurentPage.Text = (int.Parse(txtCurentPage.Text) - 1).ToString();
            }
        }
        else if (((LinkButton)sender).ID == "Next")
        {
            //Check if we can display the next page.
            if ((int.Parse(txtCurentPage.Text) * int.Parse(txtPageSize.Text)) < int.Parse(txtTotal.Text))
            {
                //Increment the CurrentPage value
                txtCurentPage.Text = (int.Parse(txtCurentPage.Text) + 1).ToString();
            }
        }
        else if (((LinkButton)sender).ID == "First")
        {
            // navigate page to the first page
            txtCurentPage.Text = 1.ToString();
        }
        else if (((LinkButton)sender).ID == "Last")
        {
            //navigate page to the last page
            int a = int.Parse(txtTotal.Text);
            int b = int.Parse(txtPageSize.Text);
            int result = a / b + 1;
            txtCurentPage.Text = result.ToString();
        }
        //Reload datalist
        BookingTableDetailsInfo bookingInfo = new BookingTableDetailsInfo();
        RestaurantInfo resInfo = new RestaurantInfo();
        bookingInfo.DateUsingTable = DateTime.Parse(txtDateUsingTable.Text);
        resInfo.ResID = drpRestaurant.SelectedValue;

        LoadData(bookingInfo, resInfo);
    }
    #endregion

    #region To enable prev/next/first/last button
    public void BuildPagers()
    {
        Prev.Visible = true;
        Next.Visible = true;
        First.Visible = true;
        Last.Visible = true;

        if (int.Parse(txtCurentPage.Text) == 1)
        {
            //Check if the first page or total record smaller the record in a page
            if (int.Parse(txtTotal.Text) <= int.Parse(txtPageSize.Text))
            {
                First.Enabled = false;
                Prev.Enabled = false;
                Next.Enabled = false;
                Last.Enabled = false;
            }
            else
            {
                First.Enabled = false;
                Prev.Enabled = false;
                Next.Enabled = true;
                Last.Enabled = true;
            }
        }
            //check if page is the last page
        else if (int.Parse(txtCurentPage.Text) == (int.Parse(txtTotal.Text) / int.Parse(txtPageSize.Text) + 1))
        {
            First.Enabled = true;
            Prev.Enabled = true;
            Next.Enabled = false;
            Last.Enabled = false;
        }
        else
        {
            First.Enabled = true;
            Prev.Enabled = true;
            Next.Enabled = true;
            Last.Enabled = true;
        }
    }
    #endregion

}
